package com.slmxz.tms.document.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.slmxz.tms.common.enums.BizMessageEnum;
import com.slmxz.tms.core.exception.Asserts;
import com.slmxz.tms.core.exception.BizException;
import com.slmxz.tms.common.result.ResultCode;
import com.slmxz.tms.document.mapper.RouteMapper;
import com.slmxz.tms.document.model.entity.Route;
import com.slmxz.tms.document.model.vo.RouteVO;
import com.slmxz.tms.document.service.RouteService;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * 线路服务实现类
 * @author sunyao
 * @Date 2024-11-08
 */
@Service
@RequiredArgsConstructor
public class RouteServiceImpl extends ServiceImpl<RouteMapper, Route> implements RouteService {

    /**
    * 获取线路分页列表
    * @return {@link IPage< RouteVO >} 线路分页列表
    */
    @Override
    public IPage<RouteVO> selectRoutePage(IPage<RouteVO> page,RouteVO route) {
        return page.setRecords(this.baseMapper.selectRoutePage(page,route));
    }
    
    /**
     * @param id 线路ID
     * @return
     */
    @Override
    public RouteVO selectRouteDetail(Long id) {
        Route entity = this.getById(id);
        RouteVO route = BeanUtil.copyProperties(entity,RouteVO.class);
        return route;
    }
    
    /**
     * 新增线路
     * @return
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean saveRoute(RouteVO route) {
        return this.save(route);
    }
    
    /**
     * 更新线路
     * @return
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean updateRoute(RouteVO route) {
        return this.updateById(route);
    }
    
    /**
     * 删除线路
     *
     * @param ids 线路ID，多个以英文逗号(,)分割
     * @return
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean deleteRoutes(String ids) {
        Asserts.fail(StrUtil.isBlank(ids), BizMessageEnum.PARAM_IS_NULL);

        // 逻辑删除
        List<Long> idList = Arrays.stream(ids.split(","))
                .map(Long::parseLong)
                .toList();
        return this.removeByIds(idList);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean activeOrLogout(RouteVO route) {
        //激活或者注销业务数据
        Asserts.fail(CollectionUtil.isEmpty(route.getIds()) || route.getIsDeleted() == null, BizMessageEnum.PARAM_IS_NULL);

        List<Route>  routeList = new ArrayList<>(route.getIds().size());
        route.getIds().forEach(id -> {
            Route routeParam = new Route();
            routeParam.setId(id);
            routeParam.setIsDeleted(route.getIsDeleted());
            routeList.add(routeParam);
        });
        this.updateBatchById(routeList,10);
        return true;
    }

}
